#include "recboxitem.h"
#include "ui_recboxitem.h"

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString &imagePath)
{
    QString style = "background-image:url(" + imagePath + ");";
    ui->recMusicImage->setStyleSheet(style);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->musicImageBox)
    {
        if(QEvent::Enter == event->type())
        {
            // 添加动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(100); // 持续时间
            animation->setStartValue(QRect(9,9,ui->recMusicImage->width(),ui->recMusicImage->height()));//动画开始位置
            animation->setEndValue(QRect(9,0,ui->recMusicImage->width(),ui->recMusicImage->height()));// 动画结束位置
            animation->start(); // 开启动画
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
            });
        }
        else if(QEvent::Leave == event->type())
        {
            // 添加动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(100); // 持续时间
            animation->setStartValue(QRect(9,0,ui->recMusicImage->width(),ui->recMusicImage->height()));
            animation->setEndValue(QRect(9,9,ui->recMusicImage->width(),ui->recMusicImage->height()));
            animation->start();
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
            });
        }
        return true;
    }

    return QObject::eventFilter(watched,event);
}
